package com.globalresources.struts.app;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.globalresources.business.inface.IMenuService;
import com.globalresources.business.inface.IUserService;
import com.globalresources.commons.AbstractAction;
import com.globalresources.commons.Constant;
import com.globalresources.commons.Pager;
import com.globalresources.commons.PagerService;
import com.globalresources.po.Menu;


/**
 * @author songduanlei@gmail.com
 * @since 创建时间：2009-7-24
 */

public class MenuAction extends AbstractAction implements ServletRequestAware{
	
	private static final long serialVersionUID = 1L;
	private List<Menu> menus;
	private IMenuService menuService;	
	private Pager pager;	
	private PagerService pagerService;
	private String currentPage;
	private String pagerMethod;
	private String totalRow;	
	private String fmessage;
	private IUserService userService;
	private Menu menu;
	
   private HttpServletRequest request;
   public void setServletRequest(HttpServletRequest request) {
              this.request = request;
       }
	
	public String list() {
		menus = new ArrayList<Menu>();
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (null != obj && obj instanceof UserDetails){
			UserDetails userDetails = (UserDetails)obj;
			GrantedAuthority[] auths = userDetails.getAuthorities();
			for (int i = 0; i < auths.length; i++) {
				GrantedAuthority auth = auths[i];
				menus.addAll(menuService.getMenus(auth.getAuthority()));
			}
		}
		return SUCCESS;
        }


	/***
	 * @author songduanlei@gmail.com
	 * list Menu
	 * @return
	 */
	public String listMenu(){
		menus=menuService.listMenus();		
		return SUCCESS;
	}
	/***
	 * @author songduanlei@gmail.com
	 * Edit Menu Info
	 * @return
	 */
	public String editMenu(){
       String id=request.getParameter("id");
       System.out.println(id);
       System.out.println("打击好大家都覅哦埃及覅哦大家啊舒服加上大");
       System.out.println(id);
       menu=(Menu)menuService.getMenuById(Integer.parseInt(id));
       System.out.println(menu.getAuthority());
		return SUCCESS;
	}
	
	/***
	 * @author songduanlei@gmail.com
	 * Save Menu Info
	 * @return
	 */
	public String saveMenu(){
		
		menuService.saveMenus(menu);
		return SUCCESS;
	}
	
	/***
	 * @author songduanlei@gmail.com
	 * SaveOrUpdate Menu Info
	 * @return
	 */
	public String SaveOrUpdateMenu(){

		
		
		
		return SUCCESS;
	}
	

	public IUserService getUserService() {
			return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IMenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}
	/**
	 * @return the menus
	 */
	public List<Menu> getMenus() {
		return menus;
	}
	/**
	 * @param menus the menus to set
	 */
	public void setMenus(List<Menu> menus) {
		this.menus = menus;
	}
	/**
	 * @return the pager
	 */
	public Pager getPager() {
		return pager;
	}
	/**
	 * @param pager the pager to set
	 */
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	/**
	 * @return the pagerService
	 */
	public PagerService getPagerService() {
		return pagerService;
	}
	/**
	 * @param pagerService the pagerService to set
	 */
	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}
	/**
	 * @return the currentPage
	 */
	public String getCurrentPage() {
		return currentPage;
	}
	/**
	 * @param currentPage the currentPage to set
	 */
	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}
	/**
	 * @return the pagerMethod
	 */
	public String getPagerMethod() {
		return pagerMethod;
	}
	/**
	 * @param pagerMethod the pagerMethod to set
	 */
	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}
	/**
	 * @return the totalRow
	 */
	public String getTotalRow() {
		return totalRow;
	}
	/**
	 * @param totalRow the totalRow to set
	 */
	public void setTotalRow(String totalRow) {
		this.totalRow = totalRow;
	}
	/**
	 * @return the fmessage
	 */
	public String getFmessage() {
		return fmessage;
	}
	/**
	 * @param fmessage the fmessage to set
	 */
	public void setFmessage(String fmessage) {
		this.fmessage = fmessage;
	}
	/**
	 * @return the serialVersionUID
	 */
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}


	/**
	 * @return the menu
	 */
	public Menu getMenu() {
		return menu;
	}


	/**
	 * @param menu the menu to set
	 */
	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	

	
	
}